using System;
using System.Linq;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Logging;
using ProjetoComperio.Framework.Web.Security;
using ProjetoComperio.Presentation.Web.GlobalResources;
using ProjetoComperio.SystemFramework.Extensions;
using ProjetoComperio.SystemFramework.Web;
using User = ProjetoComperio.DataLayer.Entities.User;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    [ExceptionLogging]
    // [RequiresAuthentication]
    public class StudentController : ComperioController
    {
        private StudentBLL StudentBLL;

        #region Ctor

        public StudentController()
        {
            StudentBLL = new StudentBLL();
        }

        public StudentController(StudentBLL studentBLL)
        {
            this.StudentBLL = studentBLL;
        }
        #endregion


        public ActionResult Index()
        {
            return View();
        }

        public override void FillEntity(Object entity, FormCollection form)
        {
            base.FillEntity(entity, form);
        }

        public ActionResult Edit()
        {
            return View();
        }

        #region Create
        [HttpGet]
        public ActionResult Create()
        {

            var academicOrganizations = new AcademicOrganizationBLL().RetrieveAcademicOrganizations().ToSelectedItemList<AcademicOrganization>("AcademicOrganizationID", "Name");
            academicOrganizations.Insert(0, new SelectListItem() { Selected = true });
            Session["AcademicOrganizations"] = academicOrganizations;

            var educationLevels = new PersonBLL().RetrieveEducationLevels().ToSelectedItemList<EducationLevel>("EducationLevelID", "Description");
            educationLevels.Insert(0, new SelectListItem() { Selected = true });
            Session["EducationLevels"] = educationLevels;

            var courses = new AcademicOrganizationBLL().RetrieveCoursesByAcademicOrganization(ISTConfig.ISTOrganizationID).ToSelectedItemList<Course>("CourseID", "Name");
            courses.Insert(0, new SelectListItem() { Selected = true });
            Session["Courses"] = courses;

            return View();
        }

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "PersonID")] Student student)
        {
            var _user = new User();
            _user.UserName = Request["UserName"];
            _user.Password = Request["Password"];
            ViewData["UserFormData"] = _user;

            if (!ModelState.IsValid)
                return View();

            var successful = StudentBLL.Create(student, Request["UserName"], Request["Password"]);
            if (!successful)
                return View(student);

            TempData["StudentID"] = student.PersonID;
            return RedirectToAction("FinishEnrollment");
        }

        #endregion

        #region FinishEnrollment
        public ActionResult FinishEnrollment(Student student)
        {
            ViewData["StudentName"] = student.Name;
            ViewData["StudentID"] = student.PersonID;
            return View();
        }

        [HttpGet]
        public ActionResult FinishEnrollment()
        {
            Student student = StudentBLL.RetrieveStudentByID(Convert.ToInt32(TempData["StudentID"]));
            ViewData["StudentName"] = student.Name;
            ViewData["StudentID"] = student.PersonID.ToString();
            return View("FinishEnrollment");
        }
        #endregion


    }


}
